package com.computergames.server.service.authentication.Implement;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.computergames.server.domain.ApiResponse;
import com.computergames.server.util.JWTUtils;
import com.computergames.server.util.StatusEnum;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.stereotype.Service;

@Service
public class VerifyServiceImpl {

    public ApiResponse<?> handleStartMessage(JsonNode startMessage){
        try {
            // 验证令牌
            String token = startMessage.get("token").asText();
            DecodedJWT verify = JWTUtils.verify(token);
            return new ApiResponse<>(StatusEnum.LOGIN_SUCCESS, "验证成功", token);
        } catch (SignatureVerificationException e) {
            e.printStackTrace();
            return new ApiResponse<>(StatusEnum.LOGIN_ERROR, "无效签名", null);
        }catch (TokenExpiredException e){
            e.printStackTrace();
            return new ApiResponse<>(StatusEnum.LOGIN_ERROR, "token过期", null);
        }catch (AlgorithmMismatchException e){
            e.printStackTrace();
            return new ApiResponse<>(StatusEnum.LOGIN_ERROR, "算法不一致", null);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResponse<>(StatusEnum.LOGIN_ERROR, "token无效", null);
        }
    }
}
